﻿using DongFangGuoKai.WMS.IApiService.ElectronicTags;
using DongFangGuoKai.WMSPlatform.Commn.AppsetingsModule;
using DongFangGuoKai.WMSPlatform.Commn.ExtendModule;
using DongFangGuoKai.WMSPlatform.Commn.HttpModule;
using DongFangGuoKai.WMSPlatform.Core.Repository;
using DongFangGuoKai.WMSPlatform.Domain.HZJLYModel;
using DongFangGuoKai.WMSPlatform.Domain.LIMSModels;
using Microsoft.Extensions.Logging;
using System.Diagnostics;
using System.Transactions;

namespace DongFangGuoKai.WMS.ApiService.ElectronicTags
{
    public class ElectronicTSpreadWallsagsService : IElectronicTSpreadWallsagsService
    {
        private readonly ICtmHttpClient ctmHttpClient;
        private readonly ILogger<ElectronicTSpreadWallsagsService> electronlogger;
        private readonly IRepository<SpreadWalls> spreadwallscellrepost;
        public string RequstUrl;

        public ElectronicTSpreadWallsagsService(ICtmHttpClient ctmHttpClient,
            ILogger<ElectronicTSpreadWallsagsService> electronlogger,
            IRepository<SpreadWalls> spreadwallscellrepost)
        {
            RequstUrl = ConfigManageHelper.Get("ElectronicUrl");
            this.ctmHttpClient = ctmHttpClient;
            this.electronlogger = electronlogger;
            this.spreadwallscellrepost = spreadwallscellrepost;
        }
        /// <summary>
        /// 请求电子标签亮灭灯
        /// </summary>
        /// <param name="successTagRequest"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool successTagResults(SuccessTagRequest successTagRequest, out string message)
        {
            //message = "点亮了";
            //return true;
            //点亮电子标签
            message = "";
            try
            {
                var data = ctmHttpClient.Post<SuccessTagResults>(RequstUrl, successTagRequest);
                if (data.code != 0)
                {
                    electronlogger.LogError("电子标签亮灭灯失败 ,入参：" + successTagRequest.ToJson() + "出参" + data.ToJson());
                    message = "请求失败：" + data.message;
                    return false;
                }
                if (data.successTagList == null || data.successTagList.Count <= 0)
                {
                    electronlogger.LogError("电子标签亮灭灯失败 ,入参：" + successTagRequest.ToJson() + "出参" + data.ToJson());
                    message = "无数据";
                    return false;
                }

                foreach (var item in data.successTagList)
                {
                    var res = spreadwallscellrepost.Get(s => s.TagIndex == item.tagIndex && s.NWall == item.nWall && s.SeqNum == item.seqNum);
                    if (successTagRequest.tagList[0].showColor == 5)
                    {
                        res.SwitchState = 0;
                    }
                    else
                    {
                        res.SwitchState = 1;
                    }
                    if (!spreadwallscellrepost.Update(res))
                    {
                        electronlogger.LogError("电子标签亮灭灯失败 ,入参：" + successTagRequest.ToJson() + "出参" + data.ToJson());
                        message = "修改状态失败";
                        return false;
                    }
                }
                electronlogger.LogInformation("电子标签亮灭灯成功 ,入参：" + successTagRequest.ToJson() + "出参" + data.ToJson());
                message = "请求成功";
                return true;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return false;
                throw;
            }

        }

        /// <summary>
        /// 分播墙灭灯通知工作台
        /// </summary>
        /// <param name="tagList"></param>
        /// <returns></returns>
        public SuccessTagResults RequestExtinguish(Extinguished tagList)
        {
            SuccessTagResults successTagResults = new SuccessTagResults();
            if (tagList.tagList is null || tagList.tagList.Count() <= 0)
            {
                successTagResults.code = 1;
                successTagResults.message = "请求参数有误";
                successTagResults.successTagList = null;
                return successTagResults;
            }
            using var trans = new TransactionScope();
            try
            {
                List<SuccessTag> successTagList = new List<SuccessTag>();
                foreach (var item in tagList.tagList)
                {
                    var res = spreadwallscellrepost.Get(s => s.TagIndex == item.tagIndex && s.NWall == item.nWall && s.SeqNum == item.seqNum);
                    res.SwitchState = 0;
                    if (!spreadwallscellrepost.Update(res))
                    {
                        trans.Dispose();
                        successTagResults.code = 1;
                        successTagResults.message = "修改状态失败";
                        successTagResults.successTagList = null;
                        return successTagResults;
                    }
                    successTagList.Add(new SuccessTag
                    {
                        nWall = item.nWall,
                        seqNum = item.seqNum,
                        tagIndex = item.tagIndex,
                    });
                }

                trans.Complete();
                
                successTagResults.code = 0;
                successTagResults.message = "提交成功";
                successTagResults.successTagList = successTagList;
                return successTagResults;
            }
            catch (Exception ex)
            {
                successTagResults.code = 1;
                successTagResults.message = "修改状态失败" + ex.Message;
                successTagResults.successTagList = null;
                return successTagResults;
                throw;
            }
            
        }
    }
}
